﻿using Newtonsoft.Json;
using System;

namespace ProjectManageSystemApi.Utilities
{
    public class LongConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(long);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            string val = reader.Value.ToString();
            if (val.IsNullOrEmpty())
            {
                return 0;
            }
            return long.TryParse(val, out long value) ? value : serializer.Deserialize(reader);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value.ToString(), typeof(string));
        }
    }
}
